home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 January: Mac OS SDK / Dev.CD Jan 99 SDK1.toast / Development Kits / Installer SDK 1.2 / Upgrader 1.2.1 & Engines / Upgrader 1.2.1 / Plug-in Examples / Common Files / Editor Utilities / EditorUtilities.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-25  |  8.6 KB  |  304 lines  |  [TEXT/CWIE]

  1.  
  2. #ifndef EditorUtilities_H
  3. #define EditorUtilities_H
  4.  
  5. #include <UKeyFilters.h>
  6.  
  7. #include <MixedMode.h>
  8. #include <Controls.h>
  9. #include <Dialogs.h>
  10. #include <Files.h>
  11. #include <Memory.h>
  12. #include <Resources.h>
  13. #include <QuickDrawText.h>
  14. #include <TextUtils.h>
  15. #include <Disks.h>
  16.  
  17. #define kEmptyString (ConstStr255Param)"\p"
  18.  
  19. // Key definitions.
  20. #define    kUpArrowKey        0x1E
  21. #define    kDownArrowKey    0x1F
  22. #define    kRightArrowKey    0x1D
  23. #define    kLeftArrowKey    0x1C
  24. #define    kPageUpKey        0x0B
  25. #define    kPageDownKey    0x0C
  26. #define    kHomeKey        0x01
  27. #define    kEndKey            0x04
  28. #define    kHelpKey        0x72
  29. #define    kTabKey            0x09
  30. #define    kBackspaceKey    0x08
  31. #define    kFwdDeleteKey    0x7F
  32. #define    kReturnKey        0x0d
  33. #define    kEnterKey        0x03
  34. #define    kEscapeKey        0x1B
  35. #define kCancelKey        0x2E
  36.  
  37.  
  38. /////////////////////////////////////////////////////////////////////////////////////////////////////
  39. //
  40. // class TRsrcHandle - class for reading and writing complex structured resources
  41. //
  42. /////////////////////////////////////////////////////////////////////////////////////////////////////
  43.     class TRsrcHandle {
  44.         public:
  45.             Handle    fRsrcHandle;
  46.             SInt32    fCurPos;        /* 1 is first position */
  47.             
  48.             TRsrcHandle();
  49.             TRsrcHandle( Handle    pInitilizedHandle );
  50.             TRsrcHandle( Ptr    pInitilizedPtr );
  51.  
  52.             virtual ~TRsrcHandle();
  53.             
  54.             // -- RESOURCE CREATION AND WRITING METHODS
  55.             void AppendLong(    SInt32    pTheLong );
  56.             void AppendShort(     SInt16    pTheShort );
  57.             void AppendStr255(     Str255    pTheStr255 );
  58.             void AppendStr255WithoutPadByte(     Str255    pTheStr255 );
  59.             void AppendStr255AsData( Str255    pTheStr255 );
  60.             void AppendStr255FromHandle( Handle    pTheHandle );
  61.             void AppendHandle(     Handle    pTheHandle );
  62.     
  63.             Handle GetRsrcHandle()    { return fRsrcHandle; };
  64.             
  65.             void Write( SInt16                    pFileRefNum,
  66.                         OSType                    pRsrcType,
  67.                         SInt16                    pRsrcID );
  68.             
  69.     
  70.             // -- RESOURCE READING METHODS
  71.             SInt32    ReadLong();
  72.             SInt16    ReadShort();
  73.             SInt16    ReadByteIntoShort();
  74.             void    ReadPStrIntoStr255( Str255 outStr255 );
  75.             Handle    ReadPStrIntoHandle();
  76.             void    ReadChunk(    Ptr        pDest,
  77.                                 SInt32     pSize );
  78.             
  79.             void    SetPos( SInt32    pNewPos )    { (fCurPos = pNewPos); };
  80.             Boolean    PastEnd()                    { return (fCurPos > GetHandleSize( fRsrcHandle ) ); };
  81.         
  82.     };
  83.  
  84. /////////////////////////////////////////////////////////////////////////////////////////////////////
  85. //
  86. // Global Routines
  87. //
  88. /////////////////////////////////////////////////////////////////////////////////////////////////////
  89.  
  90. SInt16    PStrCmp( ConstStr255Param p2, ConstStr255Param p1 );
  91.  
  92. void PStrConcat( unsigned char* dst, const unsigned char* src );
  93.  
  94. void PStrCopy( Str255 p2, ConstStr255Param p1 );
  95.  
  96. void GetIndStringWithParam( Str255                 theString,
  97.                             short                 strListID,
  98.                             short                 index,
  99.                             ConstStr255Param    pParamText0,
  100.                             ConstStr255Param    pParamText1,
  101.                             ConstStr255Param    pParamText2,
  102.                             ConstStr255Param    pParamText3 );
  103.  
  104. void WriteStringResource( SInt16 inRsrcID, Str255 inString );
  105.  
  106. void WriteStringListResourceIndex( SInt16 inFileRefNum, Str255 inString, SInt16 inRsrcID, SInt16 inIndex );
  107.  
  108. void ResetResListResource( SInt16 inFileRefNum, SInt16 inResListRsrcID );
  109.  
  110. void AppendToResListResource( SInt16 inFileRefNum, SInt16 inResListRsrcID, ResType inRsrcType, SInt16 inRsrcID );
  111.  
  112. SInt16 GetUniqueIDForResType( SInt16 inFileRefNum, ResType theResType );
  113.  
  114. void DeleteResource( SInt16 inFileRefNum, ResType inRsrcType, SInt16 inRsrcID );
  115.  
  116. OSErr GetFileNameFromFileRefID( SInt16 inFileRefID, Str255 outFileName );
  117.  
  118. EKeyStatus    PrintingCharWithReturnField(const EventRecord    &inKeyEvent );
  119.  
  120. EKeyStatus    SignedIntegerField(const EventRecord    &inKeyEvent );
  121.  
  122. /////////////////////////////////////////////////////////////////////////////////////////////////////
  123. //
  124. // Exceptions Handling Routines
  125. //
  126. /////////////////////////////////////////////////////////////////////////////////////////////////////
  127.  
  128.  
  129. //----------------------------------------------------------------------------------------
  130. // This macro can be called on any variable to keep it out of a register. It is
  131. // used specifically in exception handling. Kludge to be used till volatile or
  132. // C++ exception handling is implemented.
  133. //----------------------------------------------------------------------------------------
  134.  
  135. #define VOLATILE(a) ((void) &a)
  136.  
  137. //
  138. // try/catch/throw usage:
  139. //
  140. // Advantage:        Supported by the compiler, so it's portable
  141. // Disadvantage:    Most of our compilers don't provide exception handling
  142. //
  143. //        try
  144. //            {
  145. //            // do something
  146. //            FailErr(SomeFnReturningOSErr());    // "throw" is unconditional, so we still need a FailErr
  147. //            }
  148. //        catch(OSErr err)
  149. //            {
  150. //            // clean up
  151. //            throw(err);
  152. //            }
  153. //
  154.  
  155. void    FailErr(OSErr);
  156. void    FailMemError();
  157. void    FailResError();                                    // resNotFound is OK
  158. void    FailNil(void*);
  159. void    FailResourceNil(Handle);                        // checks QuickResError, and resource for NIL
  160.  
  161.  
  162. //----------------------------------------------------------------------------------------
  163. // FailErr: 
  164. //----------------------------------------------------------------------------------------
  165. inline void FailErr(OSErr error)
  166. {
  167.     if( error != noErr )
  168.         throw( error );
  169. }
  170.  
  171.  
  172. //----------------------------------------------------------------------------------------
  173. // FailMemError: 
  174. //----------------------------------------------------------------------------------------
  175. inline void FailMemError()
  176. {
  177.     OSErr error = MemError();
  178.     if (error != noErr)
  179.         throw( error );
  180. }
  181.  
  182. //----------------------------------------------------------------------------------------
  183. // FailNil: 
  184. //----------------------------------------------------------------------------------------
  185. inline void FailNil(void *pointer)
  186. {
  187.     if (pointer == nil)
  188.         throw( memFullErr);
  189. }
  190.  
  191. //----------------------------------------------------------------------------------------
  192. // FailResError: 
  193. //
  194. // resNotFound is OK
  195. //----------------------------------------------------------------------------------------
  196. inline void FailResError()
  197. {
  198.     OSErr error = ResError();
  199.  
  200.     if ((error != noErr) && (error != resNotFound))
  201.         throw( error );
  202. }
  203.  
  204. //----------------------------------------------------------------------------------------
  205. // FailResourceNil: 
  206. //----------------------------------------------------------------------------------------
  207. inline void FailResourceNil(Handle resource)
  208. {
  209.     OSErr error = ResError();
  210.  
  211.     if ((error != noErr) && (error != resNotFound))
  212.         throw( error );
  213.     else if (resource == nil)
  214.         throw( resNotFound );
  215. }
  216.  
  217.  
  218. /////////////////////////////////////////////////////////////////////////////////////////////////////
  219. //
  220. // Class LShortComparator
  221. //
  222. /////////////////////////////////////////////////////////////////////////////////////////////////////
  223.  
  224. class    LShortComparator : public LComparator {
  225. public:
  226.                         LShortComparator() { }
  227.     virtual                ~LShortComparator() { }
  228.             
  229.     virtual Int32        Compare(
  230.                                 const void*            inItemOne,
  231.                                 const void*         inItemTwo,
  232.                                 Uint32                inSizeOne,
  233.                                 Uint32                inSizeTwo) const;
  234.                                 
  235.     virtual Boolean        IsEqualTo(
  236.                                 const void*            inItemOne,
  237.                                 const void*         inItemTwo,
  238.                                 Uint32                inSizeOne,
  239.                                 Uint32                inSizeTwo) const;
  240.                                 
  241.     static LShortComparator*    GetComparator();
  242.     
  243. protected:
  244.     static LShortComparator*    sShortComparator;
  245. };
  246.  
  247.  
  248. /////////////////////////////////////////////////////////////////////////////////////////////////////
  249. //
  250. // Class LRsrcTypeAndIDComparator
  251. //
  252. /////////////////////////////////////////////////////////////////////////////////////////////////////
  253.  
  254. struct RsrcTypeAndID 
  255. {
  256.     ResType                fRsrcType;
  257.     SInt16                fRsrcID;
  258. };
  259. typedef struct RsrcTypeAndID RsrcTypeAndID;
  260.  
  261. class    LResTypeAndIDComparator : public LComparator {
  262. public:
  263.                         LResTypeAndIDComparator() { }
  264.     virtual                ~LResTypeAndIDComparator() { }
  265.             
  266.     virtual Int32        Compare(
  267.                                 const void*            inItemOne,
  268.                                 const void*         inItemTwo,
  269.                                 Uint32                inSizeOne,
  270.                                 Uint32                inSizeTwo) const;
  271.                                 
  272.     virtual Boolean        IsEqualTo(
  273.                                 const void*            inItemOne,
  274.                                 const void*         inItemTwo,
  275.                                 Uint32                inSizeOne,
  276.                                 Uint32                inSizeTwo) const;
  277.                                 
  278.     static LResTypeAndIDComparator*    GetComparator();
  279.     
  280. protected:
  281.     static LResTypeAndIDComparator*    sResTypeAndIDComparator;
  282. };
  283.  
  284. /////////////////////////////////////////////////////////////////////////////////////////////////////
  285. //
  286. // Editor calling definitions
  287. //
  288. /////////////////////////////////////////////////////////////////////////////////////////////////////
  289.  
  290. extern "C"{
  291. struct EditorLibProcParamBlock
  292. {
  293.     SInt16         fFormatNum;
  294.     QDGlobals*     fQDGlobals;
  295.     SInt16         fFileRefNum;
  296.     SInt16         fPreferenceRsrcID;
  297.     SInt16         fResListRsrcID;
  298. };
  299. typedef struct EditorLibProcParamBlock *EditorLibProcParamBlockPtr;
  300.  
  301. typedef  SInt32 (*EditorLibProcPtr)( EditorLibProcParamBlockPtr inEditorLibProcParamBlockPtr );
  302. }
  303.  
  304. #endif